package com.whacos.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xiongdun
 * @description 字符串处理工具类
 * @since 2018/11/17 17:11
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * @param email
     * @return
     * @description 判断字符串是否为邮箱
     * @datetime 2017年4月10日下午10:53:07
     * @author xiongdun
     */
    public static boolean isEmail(String email) {
        String patternStr = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return matcher(patternStr, email);
    }

    /**
     * @param mobileNumber
     * @return
     * @description 判断字符串是否为手机号码
     * @datetime 2017年4月10日下午10:59:49
     * @author xiongdun
     */
    public static boolean isMobileNumber(String mobileNumber) {
        String patternStr = "^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$";
        return matcher(patternStr, mobileNumber);
    }

    /**
     * @param idcard
     * @return
     * @description 判断是否为IDCard
     * @author xiongdun
     * @datetime 2017年5月16日下午10:51:54
     */
    public static boolean isIdcard(String idcard) {
        String patternStr = null;
        if (idcard.length() == 15) {
            patternStr = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        } else if (idcard.length() == 18) {
            patternStr = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$";
        }
        return matcher(patternStr, idcard);
    }

    /**
     * @param patternStr
     * @param matcherStr
     * @return
     * @description 正则表达式匹配器
     * @datetime 2017年4月10日下午11:00:12
     * @author xiongdun
     */
    public static boolean matcher(String patternStr, String matcherStr) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile(patternStr);
            Matcher matcher = regex.matcher(matcherStr);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
    /**
     * 驼峰法转下划线
     *
     * @param line 源字符串
     * @return 转换后的字符串
     */
    public static String camel2Underline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toLowerCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }

    /**
     * Returns the index within this string of the rightmost occurrence of the
     * specified substring.
     *
     * @param input
     * @param query
     */
    public static int getLastMatchingIndex(String input, String query) {
        char[] inputChars = input.toCharArray();
        char[] queryChars = query.toCharArray();
        int inputLength = input.length();
        int queryLength = query.length();
        int inputIndex = inputLength - 1;
        int queryIndex = queryLength - 1;
        int matchingLength = 0;
        while (inputIndex >= 0 && queryIndex >= 0) {
            if (inputChars[inputIndex] == queryChars[queryIndex]) {
                inputIndex--;
                queryIndex--;
                matchingLength++;
            } else {
                inputIndex = matchingLength <= 0 ? (inputIndex - 1) : inputIndex;
                queryIndex = queryLength - 1;
                matchingLength = 0;
            }
        }
        return matchingLength == queryLength ? (inputIndex + 1) : -1;
    }


    /**
     * Returns the index within the given input string of the first occurrence
     * of the specified substring.
     *
     * @param input
     * @param query
     */
    public static int getFirstMatchingIndex(String input, String query) {
        char[] inputChars = input.toCharArray();
        char[] queryChars = query.toCharArray();
        int inputLength = input.length();
        int queryLength = query.length();

        int inputIndex = 0;
        int queryIndex = 0;
        while (inputIndex < inputLength && queryIndex < queryLength) {
            if (inputChars[inputIndex] == queryChars[queryIndex]) {
                queryIndex++;
                inputIndex++;
            } else {
                inputIndex = inputIndex - queryIndex + 1;
                queryIndex = 0;
            }
        }

        return queryIndex == queryLength ? (queryLength > 1 ? inputIndex - queryLength : inputIndex - 1) : -1;
    }
}
